package com.jt.assistant.ui

import android.content.Intent
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ToastUtils
import com.jt.assistant.R
import com.jt.assistant.adapter.SkuPicAdapter
import com.jt.assistant.bean.Param
import com.jt.assistant.bean.SkuBean
import com.jt.assistant.viewmodel.HomeViewModel
import com.yzp.mvvmlibrary.base.BaseActivity
import kotlinx.android.synthetic.main.activity_sku_pic.*

/**
 * 查询SKU
 */
class SkuPicActivity : BaseActivity<HomeViewModel>() {
    private val mAdapter by lazy {
        SkuPicAdapter(
            ArrayList<SkuBean>()
        )
    }
    private var mCurrentCode: String = ""

    override fun onResume() {
        super.onResume()
        if (mCurrentCode.isNotEmpty()) {
            val params: Param = Param::class.java.newInstance()
            params.channelOrderNo = mCurrentCode
            params.sku = ""
            viewModel.getSkuByChannelOrder(params)
        }
    }

    override fun layoutId(): Int = R.layout.activity_sku_pic

    override fun initView(savedInstanceState: Bundle?) {
        with(rv_skuPic) {
            layoutManager = LinearLayoutManager(context)
            adapter = mAdapter
        }
    }

    override fun initData() {}

    override fun initListener() {
        set_skuSearch.setOnScanListener {
            if (it.isNullOrEmpty()) {
                ToastUtils.showShort("请输入渠道订单号")
                return@setOnScanListener
            }
            mCurrentCode = it
            val params: Param = Param::class.java.newInstance()
            params.channelOrderNo = it
            params.sku = ""
            viewModel.getSkuByChannelOrder(params)
        }

        mAdapter.setOnItemClickListener { adapter, view, position ->
            startActivity(Intent(this@SkuPicActivity, SkuPicDetailActivity::class.java).apply {
                putExtra("SKU_NO", mAdapter.data[position].sku)
            })
        }

        mAdapter.setOnItemChildClickListener { adapter, view, position ->
            startActivity(Intent(this@SkuPicActivity, SkuPicUploadActivity::class.java).apply {
                putExtra("SKU_NO", mAdapter.data[position].sku)
                putExtra("SKU_PIC_NUM", mAdapter.data[position].picNum)
            })
        }


        //203092285
        viewModel.mGetSkuByChannelOrder.observe(this, {
            if (it.isSuccess() && !it.data.isNullOrEmpty()) {
                mAdapter.data.clear()
                mAdapter.data.addAll(it.data)
                mAdapter.notifyDataSetChanged()
            } else {
                ToastUtils.showShort("没有数据")
            }
        })

    }


}